/**
 * 
 */
package com.bf.xpdd.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;


/**
 * JSON 转换工具类
 * 
 * @author LiuChang
 * @version 1.0 2010-12-08
 * @since 1.0
 */
public class JSONTranslator
{

	private static final Logger LOGGER = Logger.getLogger(JSONTranslator.class);

	/**
	 * 
	 */
	private static final ObjectMapper MAPPER = new ObjectMapper();

	/**
	 * 
	 */
	private JSONTranslator()
	{
		super();
	}

	/**
	 * @return
	 */
	public static ObjectMapper getInstance()
	{
		return MAPPER;
	}

	/**
	 * 将JSON串转换为bean
	 * 
	 * @param <T>
	 * @param json
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("cast")
	public static <T> T toBean(String json, Class<T> clazz)
	{
		try
		{
			return (T) MAPPER.readValue(json, clazz);
		}
		catch (JsonParseException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}
		catch (JsonMappingException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}
		catch (IOException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}

		return null;
	}

	/**
	 * 将JSON串转换为bean
	 * 
	 * @param <T>
	 * @param json
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T toCollection(String json,
			@SuppressWarnings("rawtypes") Class<? extends Collection> clazz,
			Class<?> clazzz)
	{
		try
		{
			return (T) MAPPER.readValue(json,
					TypeFactory.collectionType(clazz, clazzz));
		}
		catch (JsonParseException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}
		catch (JsonMappingException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}
		catch (IOException e)
		{
			JSONTranslator.LOGGER.error("toBean", e);
		}

		return null;
	}

	/**
	 * 将bean转换为JSON串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJSON(Object object)
	{
		StringWriter sw = new StringWriter();
		JsonGenerator gen;
		try
		{
			gen = new JsonFactory().createJsonGenerator(sw);
			JSONTranslator.MAPPER.writeValue(gen, object);
			gen.close();
			return new String(sw.toString().getBytes("utf-8"), "iso-8859-1");
			//return sw.toString();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;

	}
	
	/**
	 * 将bean转换为JSON串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJSON(Object object, String sourceCharset, String targetCharset)
	{
		StringWriter sw = new StringWriter();
		JsonGenerator gen;
		try
		{
			gen = new JsonFactory().createJsonGenerator(sw);
			JSONTranslator.MAPPER.writeValue(gen, object);
			gen.close();
			return new String(sw.toString().getBytes(sourceCharset), targetCharset);
			//return sw.toString();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;

	}
}